<?php
#####DB CONFIG ############
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('mb120')or die(mysql_error());

// Used to debug variable
function pr($array){
  echo "<pre style=\"color:#FFF;background:#000\">";
  print_r($array);
  echo "</pre>";
}
############## INSERT country ######
pr($_POST);
if(isset($_POST['name']) && !empty($_POST['name']))
{
  $c_name = $_POST['name'];
  $query = "INSERT INTO `countries`
            SET name='$c_name'";
//pr($query);
  mysql_query($query) or die(mysql_error());
  $last_insert_id = mysql_insert_id();
  echo "Country INserted on ID $last_insert_id\n\n";
}

## SELECT countries ######
$query = "SELECT * FROM `countries`";
$result = mysql_query($query) or die(mysql_error());
###################################



?>
<form name="countries" action="" method="post">
  <table width="60%" align="center" border="1">
    <tr>
      <td>Name : </td>
      <td><input type="text" name="name" required ></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="submit" value="Add country" ></td>
    </tr>
    </table>
</form>

<table width="60%" align="center" border="1">
  <thead>
    <tr>
      <th>#</th>
      <th>Name</th>
    </tr>
  <thead>

  <tbody>
<?php
  while($ans = mysql_fetch_assoc($result)){
?>
    <tr>
      <td><?=$ans['id']?></td>
      <td><?=$ans['name']?></td>
    </tr>
<?php
  }//end of while
?>

  </tbody>

</table>
